﻿@model IEnumerable<Swift.Core.JobBase>
@using Newtonsoft.Json

@{
    ViewBag.Title = "作业记录";
}

@section header{
    <link href="https://cdn.bootcss.com/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css" rel="stylesheet">
}

<h2>集群 @ViewBag.ClusterName -> @ViewBag.JobName 作业记录</h2>

<p>
    <a class="btn btn-info" href="@Url.Action("Jobs","Home",new {cluster=ViewBag.ClusterName})">返回</a>
    <a class="btn btn-info" href="javascript:location.reload()">刷新</a>
</p>

<div class="row">
    <div class="col-md-7"></div>
    <div class="col-md-5">
        <div class="form-group text-right">
            <div class='input-group date' id='datetimepicker'>
                <input type='text' class="form-control" />
                <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
            </div>
        </div>
    </div>
</div>

<table class="table table-striped table-hover">
    <tr>
        <th>
            Id
        </th>
        <th>
            作业版本
        </th>
        <th>
            创建时间
        </th>
        <th>
            完成时间
        </th>
        <th>
            状态
        </th>
        <th>
            操作
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Version)
            </td>
            <td>
                @item.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
            </td>
            <td>
                @(item.FinishedTime.Year > 2000 ? item.FinishedTime.ToString("yyyy-MM-dd HH:mm:ss") : string.Empty)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Status)
            </td>
            <td>
                <div id="taskList_@item.Id" style="        display: none">
                    <table class="table table-striped table-hover">
                        <tr>
                            <th>Id</th>
                            <th>成员</th>
                            <th>开始时间</th>
                            <th>完成时间</th>
                            <th>状态</th>
                        </tr>
                        @{
                var tasks = item.TaskPlan;
                if (tasks != null)
                {
                    foreach (var memberId in tasks.Keys)
                    {
                        var subTasks = tasks[memberId];
                        if (subTasks != null && subTasks.Any())
                        {
                            foreach (var task in subTasks)
                            {
                                <tr>
                                    <td>@task.Id</td>
                                    <td>@memberId</td>
                                    <td>
                                        @(task.StartTime.Year > 2000 ? task.StartTime.ToString("yyyy-MM-dd HH:mm:ss") : string.Empty)
                                    </td>
                                    <td>
                                        @(task.FinishedTime.Year > 2000 ? task.FinishedTime.ToString("yyyy-MM-dd HH:mm:ss") : string.Empty)
                                    </td>
                                    <td>@task.Status</td>
                                </tr>
                }
            }
        }
    }
                        }
                    </table>
                </div>
                <a href="javascript:void(0)" class="btnShowTasklist" id="btnShowTasklist_@item.Id">任务记录</a>

                @{
        if (item.Status == EnumJobRecordStatus.TaskMerged)
        {
            <a href="javascript:void(0)" class="btnDownloadJobResult" id="btnDownloadJobResult_@item.Id">下载结果</a>
}
                }

                @{
        if (item.Status != EnumJobRecordStatus.Canceling
        && item.Status != EnumJobRecordStatus.Canceled
        && item.Status != EnumJobRecordStatus.TaskExecutingFailed
        && item.Status != EnumJobRecordStatus.PlanFailed
        && item.Status != EnumJobRecordStatus.TaskMerged
        && item.Status != EnumJobRecordStatus.TaskMergeFailed)
        {
            <a href="javascript:void(0)" class="btnCancelJobRun" id="btnCancelJobRun_@item.Id">取消运行</a>
}
                }
            </td>
        </tr>
    }

</table>

<div class="modal fade" id="taskModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel"></h4>
            </div>
            <div class="modal-body">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

@section scripts{
    <script src="https://cdn.bootcss.com/moment.js/2.24.0/moment-with-locales.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
    <script src="~/lib/bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
    <script type="text/javascript">
    function refreshpage()
    {
       window.location.reload();
    }
    var refreshpagetimer = setTimeout('refreshpage()',5000);

    $('#taskModal').on('hide.bs.modal', function () {
        refreshpagetimer=setTimeout('refreshpage()',5000);
    });

    $('#taskModal').on('show.bs.modal', function () {
        clearTimeout(refreshpagetimer);
    });

    function downloadFile(url) {
        try{
            var elemIF = document.createElement("iframe");
            elemIF.src = url;
            elemIF.style.display = "none";
            document.body.appendChild(elemIF);
        }catch(e){

        }
    }

    var jobName = "@ViewBag.JobName";
    var clusterName="@ViewBag.ClusterName";

    $(function () {
        $(".btnShowTasklist").on('click', function (e) {
            var tableId = "taskList_" + $(this).attr("id").split('_')[1];
            $("#taskModal .modal-title").text("任务列表");
            $("#taskModal .modal-body").html($("#" + tableId).html());
            $('#taskModal').modal({ keyboard: false });
        });

        $(".btnDownloadJobResult").on('click', function (e) {
            var jobId = $(this).attr("id").split('_')[1];

            downloadFile("/Job/DownloadResult?clusterName="+clusterName+"&jobName="+jobName+"&jobId="+jobId);
        });

        $(".btnCancelJobRun").on('click', function (e) {
            var jobId = $(this).attr("id").split('_')[1];
            $.ajax({
                url: "@Url.Action("Cancel","Job")",
                type:"GET",
                dataType:"json",
                data:{"clusterName":clusterName,"jobName":jobName,"jobId":jobId},
                success: function(data,textStatus){
                    if(data.errCode==0){
                        $("#taskModal .modal-title").text("提交成功");
                        $("#taskModal .modal-body").text("作业取消中，请关闭此提示查看作业状态。");
                        $('#taskModal').modal({ keyboard: false });
                    }else{
                        $("#taskModal .modal-title").text("提交失败");
                        $("#taskModal .modal-body").text(data.errMessage);
                        $('#taskModal').modal({ keyboard: false });
                    }
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    alert("提交失败");
                }
            });
        });

        $('#datetimepicker').datetimepicker({
            locale: 'zh-CN',
            format: 'YYYY-MM-DD',
            defaultDate: "@ViewBag.SpecifiedDate"
        }).on("dp.change", function (e) {
            var currentHref=window.location.href.split("?")[0];
            window.location.href=currentHref+"?cluster="+clusterName+"&job="+jobName+"&date="+(moment(e.date).format("YYYY-MM-DD"));
        });
    });
    </script>
}
